﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SmallBusinessKit.DataAccess.Interfaces;
using SmallBusinessKit.Domains;
using SmallBusinessKit.Interfaces;
using SmallBusinessKit.Models.Entities;

namespace SmallBusinessKit.Tests.Domains
{
    /// <summary>
    /// Unit Tests for PeopleDomain Logic
    /// </summary>
    [TestClass]
    public class PeopleDomainTests : UnitTestBase 
    {
        #region Properties

        private Mock<IPersonRepository>   _moqPersonRepository;
        private IPersonDomain             _domain;

        #endregion Properties

        #region Tests

        [TestInitialize]
        public override void Setup()
        {
            base.Setup();

            var people = XmlData.ParsePeople();
            var qry  = people.AsQueryable().OrderBy(i => i.Id);

            _moqPersonRepository = new Mock<IPersonRepository>();
            _domain              = new PeopleDomain(_moqPersonRepository.Object);

            _moqPersonRepository.Setup(m => m.GetAsQueryable()).Returns(qry);
            _moqPersonRepository.Setup(m => m.GetById("02")).Returns(people[1]);
            _moqPersonRepository.Setup(m => m.Get(It.IsAny<Expression<Func<Person, bool>>>())).Returns(people[1]);
            _moqPersonRepository.Setup(m => m.GetMany(It.IsAny<Expression<Func<Person, bool>>>())).Returns(people);
        }

        [TestMethod]
        public void GetAllPersonsQueryable()
        {
            // Act
            var results = _domain.GetAllPersons();

            // Assert
            Assert.IsNotNull(results);
            Assert.IsInstanceOfType(results, typeof(IQueryable<Person>));
        }

        [TestMethod]
        public void GetAllPersons()
        {
            // Act
            var results = _domain.GetAllPersons().ToList();

            // Assert
            Assert.IsNotNull(results);
            Assert.IsInstanceOfType(results, typeof(IEnumerable<Person>));
            Assert.AreEqual(12, results.Count());
        }

        [TestMethod]
        public void GetAllPersonsList()
        {
            // Act
            var results = _domain.GetAllPersonsList();

            // Assert
            Assert.IsNotNull(results);
            Assert.IsInstanceOfType(results, typeof(IEnumerable<Person>));
            Assert.AreEqual(12, results.Count());
        }

        #endregion
    }
}
